<?php
class ControllerSaleWechat extends Controller {
	private $error = array();

	public function index() {

        $this->language->load('sale/wechat');

        $this->document->setTitle('微信用户管理');

        $this->load->model('sale/wechat');

        $this->getList();


	}
    protected function getList() {
        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'name';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('heading_title'),
            'href'      => $this->url->link('sale/wechat', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        $this->data['insert'] = $this->url->link('sale/wechat/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['delete'] = $this->url->link('sale/wechat/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

        $this->data['wechats'] = array();

        $data = array(
            'sort'  => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_admin_limit'),
            'limit' => $this->config->get('config_admin_limit')
        );

        $wechat_total = $this->model_sale_wechat->getTotalWechats();

        $results = $this->model_sale_wechat->getWechats($data);

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_view'),
                'href' => $this->url->link('sale/wechat/lottery', 'token=' . $this->session->data['token'] . '&openid=' . $result['openid'] . $url, 'SSL')
            );

            $this->data['wechats'][] = array(
                'openid'       => $result['openid'],
                'nickname'       => $result['nickname'],
                'sex' => $result['sex'],
                'date_added'   => $result['date_added'],
                'province'   => $result['province'],
                'city'   => $result['city'],
                'country'   => $result['country'],
                'subscribe'   => $result['subscribe'],
                'share_status'   => $result['share_status'],
                'referee_id'   => $result['referee_id'],
                'lottery_times'   => $result['lottery_times'],
                'name'   => $result['name'],
                'telephone'   => $result['telephone'],
                'action'    => $action
            );
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_no_results'] = $this->language->get('text_no_results');

        $this->data['column_openid'] = $this->language->get('column_openid');
        $this->data['column_nickname'] = $this->language->get('column_nickname');
        $this->data['column_sex'] = $this->language->get('column_sex');
        $this->data['column_date_added'] = $this->language->get('column_date_added');
        $this->data['column_province'] = $this->language->get('column_province');
        $this->data['column_city'] = $this->language->get('column_city');
        $this->data['column_country'] = $this->language->get('column_country');
        $this->data['column_subscribe'] = $this->language->get('column_subscribe');
        $this->data['column_share_status'] = $this->language->get('column_share_status');
        $this->data['column_referee_id'] = $this->language->get('column_referee_id');
        $this->data['column_action'] = $this->language->get('column_action');
        $this->data['column_lottery_times'] = $this->language->get('column_lottery_times');
        $this->data['column_name'] = $this->language->get('column_name');
        $this->data['column_telephone'] = $this->language->get('column_telephone');

        $this->data['button_insert'] = $this->language->get('button_insert');
        $this->data['button_delete'] = $this->language->get('button_delete');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $url = '';

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['sort_openid'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=openid' . $url;
        $this->data['sort_nickname'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=nickname' . $url;
        $this->data['sort_sex'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=sex' . $url;
        $this->data['sort_province'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=province' . $url;
        $this->data['sort_city'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=city' . $url;
        $this->data['sort_country'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=country' . $url;
        $this->data['sort_subscribe'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=subscribe' . $url;
        $this->data['sort_share_status'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=share_status' . $url;
        $this->data['sort_referee_id'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=referee_id' . $url;
        $this->data['sort_date_added'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=date_added' . $url;
        $this->data['sort_lottery_times'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=lottery_times' . $url;
        $this->data['sort_name'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=name' . $url;
        $this->data['sort_telephone'] = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . '&sort=telephone' . $url;

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $pagination = new Pagination();
        $pagination->total = $wechat_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = HTTPS_SERVER . 'index.php?route=sale/wechat&token=' . $this->session->data['token'] . $url . '&page={page}';

        $this->data['pagination'] = $pagination->render();

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->template = 'sale/wechat_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function lottery(){
        echo "<pre>";
        $this->load->model('sale/wechat');
        print_r($this->model_sale_wechat->getuserinfo($this->request->get['openid']));
        print_r($this->model_sale_wechat->getuserLotteryinfo($this->request->get['openid']));
    }
    public function delete() {
        $this->language->load('sale/wechat');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/wechat');

        if (isset($this->request->post['selected']) && $this->validateDelete()) {
            foreach ($this->request->post['selected'] as $wechat_id) {
                $this->model_sale_wechat->deletewechat($wechat_id);
            }

            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('sale/wechat', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getList();
    }
    protected function validateDelete() {
        if (!$this->user->hasPermission('modify', 'sale/wechat')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

}
?>